﻿// -----------------------------------------------------------------------
// <copyright file="Extensions.cs" company="Spardo, LLC">
// Copyright (c) 2011 SPardo, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a 
// copy of this software and associated documentation files (the "Software"), 
// to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the 
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included 
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
// </copyright>
// -----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using BBYOpen.Linq;
using BBYOpen.Linq.Models;

/// <summary>
/// TODO: Update summary.
/// </summary>
public static class Extensions
{
    public static T Get<T>(this IQueryable<T> source, int id) where T : IRemixItem
    {
        if (source == null)
            throw new ArgumentNullException("source");

        var method = Expression.Call(typeof(Extensions), "Get"
            , new Type[] { source.ElementType }, new Expression[] { source.Expression, Expression.Constant(id) });

        return source.Provider.Execute<T>(method);
    }

    public static IQueryable<T> Page<T>(this IQueryable<T> source, int count) where T : IRemixItem
    {
        if (source == null)
            throw new ArgumentNullException("source");

        var method = Expression.Call(typeof(Extensions), "Page"
            , new Type[] { source.ElementType }, new Expression[] { source.Expression, Expression.Constant(count) });

        return source.Provider.CreateQuery<T>(method);
    }

    public static IQueryable<T> Search<T>(this IQueryable<T> source, string criteria) where T : Product
    {
        if (source == null)
            throw new ArgumentNullException("source");

        var method = Expression.Call(typeof(Extensions), "Search"
            , new Type[] { source.ElementType }, new Expression[] { source.Expression, Expression.Constant(criteria) });

        return source.Provider.CreateQuery<T>(method);
    }

    internal static IQueryable<T> Area<T>(this IQueryable<T> source, object[] values) where T : Store
    {
        if (source == null)
            throw new ArgumentNullException("source");

        var trees = new List<Expression>();

        foreach (var value in values)
            trees.Add(Expression.TypeAs(Expression.Constant(value), typeof(object)));

        var array = Expression.NewArrayInit(typeof(object), trees);

        var method = Expression.Call(typeof(Extensions), "Area"
            , new Type[] { source.ElementType }, new Expression[] { source.Expression, array });

        return source.Provider.CreateQuery<T>(method);
    }

    public static IQueryable<T> Area<T>(this IQueryable<T> source, string postalCode, int miles) where T : Store
    {
        return source.Area(new object[] { postalCode, miles });
    }

    public static IQueryable<T> Area<T>(this IQueryable<T> source, decimal latitude, decimal longitude, int miles) where T : Store
    {
        return source.Area(new object[] { latitude, longitude, miles });
    }

    public static IQueryable<T> Area<T>(this IQueryable<T> source, decimal latitude, decimal longitude) where T : Store
    {
        return source.Area(new object[] { latitude, longitude });
    }

    public static Pageable<T> ToPageable<T>(this IEnumerable<T> source) where T : IRemixItem
    {
        Pageable<T> output = null;

        var query = source as RemixQueryable<T>;

        if (query != null)
            output = query.GetPageable();

        return output;
    }
}
